#include "fns.h"

#include <math.h>
#include <string.h>

/** Trignometric sin() function.  Argument in radians. */
				static double 
sinFn(double x) 
{ 
				return sin(x); 
}

/** Function from <http://en.wikipedia.org/wiki/Integral>.  Integral from
 *  -2 to +2 is 3.76.
 */
				static double 
wikipediaFn(double x) 
{
				return 0.2 * (0.01*(322 + 3*x*(98 + x * (37 + x))) - 24*x/(1 + x*x));
}

/** Structure giving information for a function. */
typedef struct {
				const char *name;	/** the function name */
				FnP f;		/** pointer to the function code */
} FnDef;

/** Array mapping function names to definitions. */
static FnDef fns[] = {
				{ "sin", sinFn },
				{ "wikipedia", wikipediaFn },
};

/** # of entries in fns[] array. */
#define N_FNS (sizeof(fns) / sizeof(fns[0]))

/** Return a function pointer to the code for the function with the
 *  the NUL-terminated name fnName. 
 */
				FnP 
getFnForString(const char *fnName)
{
				return getFnForStringLen(fnName, strlen(fnName));
}

/** Return a function pointer to the code for the function with the
 *  the name fnName (not necessary NUL-terminated) having fnNameLen chars. 
 */
				FnP 
getFnForStringLen(const char *fnName, int fnNameLen)
{
				for (int i = 0; i < N_FNS; i++) {
								if (strncmp(fnName, fns[i].name, fnNameLen) == 0 &&
																fns[i].name[fnNameLen] == '\0') {
												return fns[i].f;
								}
				}
				return NULL;
}





